28B - pSort - CodeForces Solution


dfs and similar dsu graphs *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define w     \
    int t;    \
    cin >> t; \
    while (t--)
#define v(n) \
    int n;   \
    cin >> n;
#define pb push_back
#define vi(name, size)             \
    vector<int> name(size);        \
    for (int i = 0; i < size; i++) \
        cin >> name[i];
#define f(n) for (int i = 0; i < n; i++)
#define all(vv) vv.begin(), vv.end()
#define pri(vv)                             \
    f(vv.size()) cout << vv[i] << char(32); \
    cout << endl;
const int N = 555555;
int fa[N], n, a[N], d[N];
int find(int x)
{
    return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;

    for (int i = 0; i <= n * n; i++)
    {
        fa[i] = i;
    }

    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }

    for (int i = 1; i <= n; i++)
    {
        cin >> d[i];

        int l = i - d[i], r = i + d[i];

        if (l >= 1)
        {
            fa[find(l)] = find(i);
        }

        if (r <= n)
        {
            fa[find(r)] = find(i);
        }
    }

    for (int i = 1; i <= n; i++)
    {
        if (find(a[i]) != find(i))
        {
            cout << "NO";

            return 0;
        }
    }

    cout << "YES";

    return 0;
}


Comments

Submit
0 Comments
More Questions

1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push